//
//  MyTakeFilmCell.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2018/1/14.
//  Copyright © 2018年 Lwx. All rights reserved.
//

#import "MyTakeFilmCell.h"
#import "TravelLikeButton.h"
#import <AVFoundation/AVAsset.h>
#import <AVFoundation/AVAssetImageGenerator.h>
#import <AVFoundation/AVTime.h>

#define collectionBtnW 60
@interface MyTakeFilmCell()
@property (nonatomic, strong)UIImageView * contentImage;
@property (nonatomic, strong)TravelLikeButton * collectionBtn;
@property (nonatomic, strong)UILabel * titleLabel;
@property (nonatomic, strong)NSString * filmID;
@property (nonatomic, strong)NSString * vic;
//@property (nonatomic, strong)AVPlayerItem * playerItem;
@end
@implementation MyTakeFilmCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = getColor(@"e5e5e5");
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.contentImage];
        [self.contentView addSubview:self.collectionBtn];
        [self.contentView addSubview:self.titleLabel];
        
    }return self;
}

- (TravelLikeButton *)collectionBtn{
    if (!_collectionBtn) {
        _collectionBtn = [[TravelLikeButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - collectionBtnW - 10, self.titleLabel.top - 10 - 20, collectionBtnW, 20)];
        [_collectionBtn setBackgroundColor:[UIColor clearColor]];
        [_collectionBtn setTitle:@"3321" forState:UIControlStateNormal];
        [_collectionBtn setImage:[UIImage imageNamed:@"icon_shoucang"] forState:UIControlStateNormal];
        [_collectionBtn setImage:[UIImage imageNamed:@"icon_yishoucang"] forState:UIControlStateSelected];
        [_collectionBtn addTarget:self action:@selector(collectionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }return _collectionBtn;
}

- (UIImageView *)contentImage{
    if (!_contentImage) {
        _contentImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 171/HEIGHT_5S_SCALE)];
        _contentImage.image = [UIImage imageNamed:@"img_list2111"];
    }return _contentImage;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.contentImage.bottom, SCREEN_WIDTH, 39/HEIGHT_5S_SCALE)];
        _titleLabel.backgroundColor = [UIColor whiteColor];
        _titleLabel.text = @"XXXXXXXXXX";
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }return _titleLabel;
}



- (void)collectionBtnClick:(TravelLikeButton *)btn{
    btn.selected = !btn.selected;
    if (self.likeBtnBlock) {
        self.likeBtnBlock();
    }
}


- (void)setModelData:(MyTakeFilmModelData *)modelData{
    _modelData = modelData;
    self.filmID = modelData.filmID;
    [self.contentImage sd_setImageWithURL:[NSURL URLWithString:modelData.video_pic]];
    self.titleLabel.text = modelData.content;
    self.vic = modelData.vic;
    [self.collectionBtn setTitle:modelData.likes forState:UIControlStateNormal];
//    UIImage * image = [UIImage getImageFromVideoURL:[NSURL URLWithString:modelData.vic]];
//    self.contentImage.image = image;
}

- (UIImage*)getVideoPreViewImage:(NSURL *)path
{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:path options:nil];
    AVAssetImageGenerator *assetGen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    
    assetGen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [assetGen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *videoImage = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    return videoImage;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
